package top.api.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import top.api.common.ResUlt;
import top.api.domain.dto.PageDto;
import top.api.domain.pojo.User;
import top.api.domain.vo.PageVo;
import top.api.service.UserService;
import top.api.mapper.UserMapper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
* @author 86191
* @description 针对表【user】的数据库操作Service实现
* @createDate 2023-12-12 10:55:47
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Autowired
    private  UserMapper userMapper;
    @Override
    public List<User> list() {
        return super.list();
    }

    @Override
    public ResUlt<?> listPage(PageDto pageDto,Integer id ,String username) {
        IPage<User> page = new Page(pageDto.getPageNum(),pageDto.getPageSize());
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(username),User::getUsername,username);
        lqw.eq(ObjectUtils.isNotEmpty(id),User::getId,id);
        userMapper.selectPage(page,lqw);
        PageVo<User> pagevo = new PageVo<>(page.getRecords(),page.getTotal());
        return ResUlt.success(pagevo);
    }

    @Override
    public ResUlt<?> add(User user) {
        if(Objects.isNull(userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getUsername,user.getUsername())))){
            userMapper.insert(user);
            return ResUlt.success();
        }
        return ResUlt.error("该用户已存在哦");
    }

    @Override
    public ResUlt<?> getOne(Integer id) {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ObjectUtils.isNotEmpty(id),User::getId,id);
        User user = userMapper.selectOne(lqw);
        return ResUlt.success(user);
    }

    @Override
    public ResUlt<?> delete(List id) {
        String loginId = StpUtil.getLoginId().toString();

        for (Object o : id) {
            if (o.toString().equals(loginId)) {
                return ResUlt.error("该登录中的账户，不能删除哦");
            }
        }
            int delete = userMapper.deleteBatchIds(id);
                return ResUlt.success(delete);

        }

    @Override
    public ResUlt<?> changeUser(User user) {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ObjectUtils.isNotEmpty(user.getId()),User::getId,user.getId());
        int update = userMapper.update(user, lqw);
        if(update>0){
            return ResUlt.success();
        }
        return ResUlt.error();
    }

    @Override
    public ResUlt<?> changeStatus(User user) {

        String loginId = StpUtil.getLoginId().toString();
        if("1".equals(loginId)){
            LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
            lqw.eq(ObjectUtils.isNotEmpty(user.getId()),User::getId,user.getId());
            int update = userMapper.update(user, lqw);
            if(update>0){
                return ResUlt.success("更改成功！");
            }
        }
        return ResUlt.error("该账户不是管理员，不可以解禁操作哦");
    }


}






